import {ref} from 'vue';
import dayjs from 'dayjs';
import $url from '@/sheep/url';

// 格式化销量
export function formatSales(type, num) {
    num = num + '';
    if (type === 'exact') {
        return '已售' + num;
    } else {
        if (num < 10) {
            return '销量≤10';
        } else {
            let a = Math.pow(10, num.length - 1);
            return '已售' + parseInt(num / a) * a + '+';
        }
    }
}

// 格式化兑换量
export function formatExchange(type, num) {
    num = num + '';
    if (type === 'exact') {
        return '已兑换' + num;
    } else {
        if (num < 10) {
            return '已兑换≤10';
        } else {
            let a = Math.pow(10, num.length - 1);
            return '已兑换' + parseInt(num / a) * a + '+';
        }
    }
}

// 格式化库存
export function formatStock(type, num) {
    num = num + '';
    if (type === 'exact') {
        return '库存' + num;
    } else {
        if (num < 10) {
            return '库存≤10';
        } else {
            let a = Math.pow(10, num.length - 1);
            return '库存 ' + parseInt(num / a) * a + '+';
        }
    }
}

// 格式化价格
export function formatPrice(e) {
    return e.length === 1 ? e[0] : e.join('~');
}

// 格式化商品轮播
export function formatGoodsSwiper(list) {
    let swiper = [];
    list.forEach((item, key) => {
        if (item.indexOf('.avi') !== -1 || item.indexOf('.mp4') !== -1) {
            swiper.push({
                src: $url.cdn(item),
                type: 'video',
            });
        } else {
            swiper.push({
                src: $url.cdn(item),
                type: 'image',
            });
        }
    });
    return swiper;
}

export function formatOrderColor(type) {
    if (
        type === 2 ||
        type === 1
    ) {
        return 'warning-color';
    } else if (
        type === 4 ||
        type === 5 ||
        type === 6
    ) {
        return 'danger-color';
    } else if (type === 3) {
        return 'success-color';
    } else if (type === 0) {
        return 'info-color';
    }
}

// 计算相隔时间
export function useDurationTime(toTime, fromTime = '') {
    toTime = getDayjsTime(toTime);
    if (fromTime === '') {
        fromTime = dayjs();
    }
    let duration = ref(toTime - fromTime);
    if (duration.value > 0) {
        setTimeout(() => {
            if (duration.value > 0) {
                duration.value -= 1000;
            }
        }, 1000);
    }

    let durationTime = dayjs.duration(duration.value);
    return {
        h: (durationTime.months() * 30 * 24 + durationTime.days() * 24 + durationTime.hours())
            .toString()
            .padStart(2, '0'),
        m: durationTime.minutes().toString().padStart(2, '0'),
        s: durationTime.seconds().toString().padStart(2, '0'),
        ms: durationTime.$ms,
    };
}

function getDayjsTime(time) {
    time = time.toString();
    if (time.indexOf('-') > 0) {
        // 'date'
        return dayjs(time);
    }
    if (time.length > 10) {
        // 'timestamp'
        return dayjs(parseInt(time));
    }
    if (time.length === 10) {
        // 'unixtime'
        return dayjs.unix(parseInt(time));
    }
}

export function getOrderStatusName(status) {
    switch (status) {
        case 0:
            return '待付款';
        case 1:
            return '待发货';
        case 2:
            return '待收货'
        case 3:
            return '已完成'
        case 4:
            return '已取消'
        case 5:
            return '无效订单'
        case 6:
            return '已合并'
    }
}

export function getOrderCutStatusName(status) {
    switch (status) {
        case 0:
            return '代切中';
        case 1:
            return '代切失败';
        case 2:
            return '代切成功';
        case 4:
            return '日本仓库收货';
    }
}

export function getExpressStatusName(status) {
    switch (status) {
        case 0:
            return '审核中';
        case -1:
            return '审批拒绝';
        case -3:
            return '收集中';
        case 1:
            return '审核通过-待拍摄打包图';
        case 2:
            return '拍摄完成-待设置运费';
        case 3:
            return '运费已设置-待付款';
        case 4:
            return '已付款-待发货';
        case 5:
            return '已发货-待收货';
        case 6:
            return '已完成';
    }
}

function isRealEmpty(val) {
    return val === null || val === undefined || val === 'null' || val === 'undefined' || val === '';
}

export function getMergeStatus(order) {
    if (!isRealEmpty(order.sendType)) {
        return order.sendType === 0 ? '直邮-' + order.channelName : '拼邮';
    } else {
        return '未确认邮寄方式';
    }
}

export function getSendTypeStatus(order) {
    if (!isRealEmpty(order.sendType)) {
        return order.sendType === 0 ? '直邮-' + order.channelName : '拼邮';
    } else {
        return '未确认邮寄方式';
    }
}

export function getPlatformsText(item) {
    let platform = item.platformType
    switch (platform) {
        case 0:
            return '煤炉';
        case 1:
            return '骏河屋';
        case 2:
            return '雅虎';
        case 4:
            return '亚马逊';
        case 5:
            return '乐天';
        case 6:
            return '其他';
    }
}

export function getOrderStatusText(order) {
    let orderStatus = order.status;
    let procureStatus = order.procureStatus
    let freightPayStatus = order.freightPayStatus
    let aftersaleStatus = order.aftersaleStatus

    // 待付款
    if (orderStatus == 0 && aftersaleStatus == 1) {
        return '待付款'
    }
    if (orderStatus == 1 && aftersaleStatus == 1 && (procureStatus == 0 || procureStatus == 1)) {
        return '中'
    }
    if (orderStatus == 1 && aftersaleStatus == 1 && procureStatus == 2 && (isRealEmpty(freightPayStatus))) {
        return '已切成'
    }
    if (orderStatus == 1 && aftersaleStatus == 1 && procureStatus == 4 && (isRealEmpty(freightPayStatus))) {
        return '已到库'
    }
    if (orderStatus == 1 && aftersaleStatus == 1 && freightPayStatus == 0) {
        return '待付运费'
    }
    if (orderStatus == 1 && aftersaleStatus == 1 && freightPayStatus == 2) {
        return '待发货'
    }
    if (orderStatus == 2 && aftersaleStatus == 1) {
        return '已发货'
    }
    if (aftersaleStatus != 1) {
        return '退款中'
    }
}

export function getOrderAfterSaleStatusName(status) {
    //1：无售后或售后关闭，2：售后处理中，3：退款中，4： 退款成功 5-退款失败
    switch (status) {
        case 1:
            return '无退款申请';
        case 2:
            return '退款审核中';
        case 3:
            return '退款中'
        case 4:
            return '退款成功'
        case 5:
            return '退款失败'
    }
}
